﻿using System; 
using System.Collections.Generic;
using System.Text;
using XXX.Models;
using fuzhu;
using System.Data; 
using XXX.Models.XXXEntities;
using LH.U8API.Bus;
using ADODB;
using XXX.Common;
using System.Data.SqlClient;
using GLY_XieTong;
using System.ServiceModel;
using ServiceReference1;
using System.Xml.Linq;
using System.Linq;
using U8COWeb;
using LH.Model.PU;
using LH.Model.ST; 
using SqlSugar; 
using Microsoft.EntityFrameworkCore.Metadata;
using Org.BouncyCastle.Ocsp;
using System.ComponentModel.DataAnnotations;
using Org.BouncyCastle.Crypto;
using System.Security.AccessControl;
using XXX.Bo; 
using Org.BouncyCastle.Bcpg.OpenPgp;
using Microsoft.AspNetCore.Mvc;
using System.Runtime.ConstrainedExecution;
using SqlSuarEntity; 
using System.Security.Cryptography;
using Org.BouncyCastle.Asn1.Esf; 
namespace XXX.Bo
{
    public static class HuaJinService
    {

       

        #region 根据生产订单生成产成品入库单
        /// <summary>
        /// 生成产成品入库单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public static RetModelbool ST_Rd10ByMO(HuaJinRdrecord model, int bRed)
        {
            try
            {
                string conStr = CanShu.conStr;

                if (model.itemList.Count == 0)
                {
                    throw new Exception("没有明细数据，无法保存");
                }
                if (string.IsNullOrEmpty(model.cHandler))
                {
                    throw new Exception("审核人为空，无法保存");
                }
                RetModel retModel = new RetModel();
                //检查是否存在
                string sql = string.Format("select count(*) sl from rdrecord10 where ccode='{0}'", model.cCode);
                int icnt = DbHelper.GetDbInt(DbHelper2.ExecuteScalar(sql, conStr));
                if (icnt > 0)
                {
                    //throw new Exception("该任务已执行");
                    return DBService.ReOKbool("单据号已存在");
                }

                //插入U8
                try
                {

                    ST_Rdrecord10.Rd10Head rdHead = new ST_Rdrecord10.Rd10Head();
                    rdHead.bWireless = "1"; //增加入库速度
                    rdHead.cvouchtype = "10";
                    rdHead.crdcode = model.cRdCode;
                    rdHead.cbustype = "成品入库";
                    rdHead.csource = "生产订单";
                   
                    rdHead.cwhcode = model.cWhCode;
                    rdHead.ddate = DbHelper.GetDbDate(model.dDate).ToString("yyyy-MM-dd");
                    rdHead.ccode = model.cCode;
                    rdHead.cmaker = model.cMaker;
                    rdHead.cdepcode = model.cDepCode;
                    rdHead.cpersoncode = model.cPersonCode;

                    rdHead.cmemo = model.cMemo;
                    //rdHead.idiscounttaxtype = "0";
                    //rdHead.bredvouch = "0";

                    rdHead.brdflag = "1";

                    rdHead.iPrintCount = "0";
           


                    List<LH.Model.ST.ST_Rdrecord10.Rd10Body> bodies = new List<LH.Model.ST.ST_Rdrecord10.Rd10Body>();

                    for (int i = 0; i < model.itemList.Count; i++)
                    {
                        LH.Model.ST.ST_Rdrecord10.Rd10Body body = new LH.Model.ST.ST_Rdrecord10.Rd10Body();
                        body.irowno = model.itemList[i].iRowNo;
                        body.cinvcode = model.itemList[i].cInvCode;
                        body.cbatch = model.itemList[i].cBatch;
                        body.dmadedate = model.itemList[i].dMDate;
                        body.dvdate = model.itemList[i].dVDate;
                        ////表体自定义项3：客户批次号
                        //body.cdefine24 = model.ItemList[i].cCusBatch;
                        ////表体自定义项9：客户片号
                        //body.cdefine30 = model.ItemList[i].cCusPiNO;
                        ////表体自定义项8：盒号
                        //body.cdefine29 = model.ItemList[i].cHJBox;
                        ////表体自定义项4：ring尺寸
                        //body.cdefine25 = model.ItemList[i].cRingSize;
                        ////表体扩展自定义项2：ring数量
                        //body.cbdefine2 = model.ItemList[i].iRingQty;
                        ////生产批次
                        //body.cbdefine5 = model.ItemList[i].cBatch;

                        if (bRed == 1)
                        {
                            body.iquantity = DbHelper.GetDbString(DbHelper.GetDbdecimal(model.itemList[i].iQuantity) * (-1));
                        }
                        else
                        {
                            body.iquantity = model.itemList[i].iQuantity;
                        }
                        if (string.IsNullOrEmpty(model.itemList[i].cMoCode))
                        {
                            throw new Exception(string.Format("第{0}行生产订单号为空", (i + 1).ToString()));
                        }
                         
                        body.cmocode = model.itemList[i].cMoCode;
                        body.imoseq = model.itemList[i].cMoRowNo;
                        body.editprop = "A"; 
                        bodies.Add(body);
                    }



                    LH.Model.ST.ST_Rdrecord10 entity = new LH.Model.ST.ST_Rdrecord10(rdHead, bodies);
                    string cRe = HuaJin_GetInfo.GetST10ByMOInfo(entity, conStr);


                    if (cRe != "ok")
                    {
                        throw new Exception(cRe);
                    }
                    string cerr, cid, ccode;
                    string xmlconfig = XmlOperation.GetConfigXML();
                    if (!Rdrecord10DALCO.AddVerifyRdrecord10(entity, xmlconfig, out cerr, out cid, out ccode))
                    { //检查是否有采购入库单生成，如果有，则删除
                        string cerr2 = "";
                        Rdrecord10DALCO.VerifyRdrecord10(cid, xmlconfig, "delete", out cerr2);
                        throw new Exception(cerr);
                    }
                    sql = string.Format(@"update RDRECORD10  set CHANDLER='{0}',iswfcontrolled = 0 where id='{1}'", model.cHandler, cid);
                    DbHelper2.ExecuteNonQuery(sql, conStr);
                    return DBService.ReOKbool();
                }
                catch (Exception ex)
                {
                    return DBService.ReErrorbool(ex.Message);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrorbool(ex.Message);
            }




        }
        #endregion

       


      

    }
}
 

    




 
